package ch.zbw.timeline.service;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;

import javax.swing.text.JTextComponent;

/**
 * Creates printable documents
 * 
 * @author Daniel Stefanovic
 * @version 19.01.2012
 * 
 */
public class TextPrintable implements Printable {
	private JTextComponent text;

	/*
	 * (non-Javadoc)
	 * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int)
	 */
	@Override
	public int print(Graphics g, PageFormat pageFormat, int pageIndex) {

		if (pageIndex >= 1)
			return Printable.NO_SUCH_PAGE;

		((Graphics2D) g).translate(pageFormat.getImageableX(), pageFormat.getImageableY());
		text.print(g);
		return Printable.PAGE_EXISTS;
	}

	/**
	 * Set the text component 
	 * 
	 * @param text text component
	 */
	public void setText(JTextComponent text) {
		this.text = text;
	}
}
